const checkTime = t => {
    //处理倒计时格式 返回两个元素的数组
    if (t < 10) {
        return [0, parseInt(t)];
    } else {
        let tArr = t.toString().split('').map((item) => parseInt(item));
        return tArr;
    }
};
// import mixin from '../../mixins'
const mixin = require('../../mixins');

// const { mfutil } = getApp()
Component({
    behaviors: [mixin],
    data: {
        visible: false,
        countDownArr: [],
    },
    properties: {
      mf_data: {
        "type": Object,
        "value": null
      },
    },
    created() {
        const { mf_data } = this.properties;
        //倒计时时间 单位：s
        this.duration = Number(mf_data.duration) / 1000 || '15';
        this.initData();
    },
    attached() {
        const { delay } = this.properties.mf_data;
        setTimeout(() => {
            this.setData({
                visible: true
            }, () => {
                // 弹窗打开之后再开始倒计时
                setTimeout(() => {
                    this.startCountdown();
                }, 500)
            })
        }, Number(delay));
    },
    detached() { },
    methods: {
        // 初始化数据
        initData() {
            const { mf_data } = this.properties;
            let obj = mf_data.children.reduce((pre, cur) => {
                if (cur.isModal || cur.isArrow) {
                    cur.className == '弹框倒计时图片' && (pre['countDownImg'] = cur);
                    cur.className == '弹框关闭图片' && (pre['closeImg'] = cur);
                } else {
                    pre['children'].push(cur);
                }
                return pre;
            }, { countDownImg: {}, closeImg: {}, children: [] });
            this.setData({
                modalObj: obj,
                countDownArr: checkTime(this.duration)
            })
        },
        closeModal() {
            this.timer && clearInterval(this.timer);
            this.setData({
                visible: false
            })
            // mfutil.utoolsParse.processEvent(this.data.modalObj.closeImg.EventList, this.data.modalObj.closeImg);
        },
        startCountdown() {
            this.timer = setInterval(() => {
                if (this.duration == 1) {
                    clearInterval(this.timer);
                    this.setData({
                        visible: false
                    })
                    return;
                } else {
                    this.duration -= 1;
                    this.setData({
                        visible: true,
                        countDownArr: checkTime(this.duration)
                    })
                }
            }, 1000);
        },
    },
});